use "./b_long.dta"
use "./s_long.dta"
use "./p_long.dta"


xtset c_code year
gen dlnsh = d.lnsh
gen dlnuv = d.lnuv
egen avedlnsh = mean(dlnsh), by(year) // item average for every year
egen avedlnuv = mean(dlnuv), by(year) // item average for every year
gen sit = dlnsh - avedlnsh
gen pit = dlnuv - avedlnuv
gen yit = (pit)^2
gen x1it = (sit)^2
gen x2it = pit*sit
tabulate c_code, generate(c) // create item dummy vars
tabulate year, generate(y) // create year dummy vars

* Feenstra Ordinary if 2sls; Narrow if gmm
ivregress gmm yit (x1it x2it = c1-c89) if inrange(year, 1994, 2018), nocons  

* Feenstra Two-way
*ivregress gmm yit (x1it x2it = y1-y26) if inrange(year, 1994, 2018), nocons
*ivregress gmm yit (x1it x2it = y1-y26 c1-c89) if inrange(year, 1994, 2018), nocons

nlcom (sigma1: (_b[x2it]+sqrt(_b[x2it]^2+4*_b[x1it]))/(2*_b[x1it])+1)
nlcom (sigma2: (_b[x2it]-sqrt(_b[x2it]^2+4*_b[x1it]))/(2*_b[x1it])+1)

nlcom (beta1: (-_b[x2it]-sqrt(_b[x2it]^2+4*_b[x1it]))/(2*_b[x1it]))
nlcom (beta2: (-_b[x2it]+sqrt(_b[x2it]^2+4*_b[x1it]))/(2*_b[x1it]))
nlcom (alpha1: (_b[x2it]+sqrt(_b[x2it]^2+4*_b[x1it]))/(2))
nlcom (alpha2: (_b[x2it]-sqrt(_b[x2it]^2+4*_b[x1it]))/(2))

















/* Swine alpha (Feenstra)
------------------------------------------------------------------------------
         yit |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       alpha |  -.1928665   .0397706    -4.85   0.000    -.2708155   -.1149175
------------------------------------------------------------------------------
------------------------------------------------------------------------------
         yit |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       alpha |   .0672216   .0190536     3.53   0.000     .0298773    .1045659
------------------------------------------------------------------------------



Bovine alpha
------------------------------------------------------------------------------
         yit |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       alpha |  -.4214747   .0242767   -17.36   0.000    -.4690562   -.3738932
------------------------------------------------------------------------------
------------------------------------------------------------------------------
         yit |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       alpha |   .0303753    .003526     8.61   0.000     .0234645    .0372861
------------------------------------------------------------------------------


Poultry alpha
------------------------------------------------------------------------------
         yit |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       alpha |  -.3578119   .0848544    -4.22   0.000    -.5241235   -.1915002
------------------------------------------------------------------------------
------------------------------------------------------------------------------
         yit |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       alpha |   .1596819   .0297542     5.37   0.000     .1013646    .2179991
------------------------------------------------------------------------------




